package com.kxf.checkapksoplugin.util

import java.net.URL
import java.util.jar.Manifest

/**
 * @author xuefeng.kuang
 * @className Utils
 * utils
 * @date 2023/09/11 17:44
 */
object Utils {
    fun getManifestValue(name: String?): String? {
        var result: String? = null
        val clazz: Class<*> = Utils::class.java
        val className = clazz.simpleName + ".class"
        val classPath = clazz.getResource(className).toString()
        if (!classPath.startsWith("jar")) {
            // Class not from JAR
            return result
        }
        val manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) +
            "/META-INF/MANIFEST.MF"
        var manifest: Manifest? = null
        try {
            manifest = Manifest(URL(manifestPath).openStream())
        } catch (e: Exception) {
            e.printStackTrace()
        }
        if (manifest == null){
            return result
        }
        val attr = manifest.mainAttributes
        result = attr.getValue(name)
        return result
    }
}